<template>
  <div class="login-container">
    <!-- 背景动态元素 -->
    <div class="background-elements">
      <!-- 几何图形 -->
      <div class="geometric-shapes">
        <div class="triangle"></div>
        <div class="circle"></div>
        <div class="hexagon"></div>
        <div class="square"></div>
        <div class="diamond"></div>
      </div>

      <!-- AI相关图标 -->
      <div class="subject-icons">
        <div class="icon">🤖</div>
        <div class="icon">💻</div>
        <div class="icon">🧠</div>
        <div class="icon">⚡</div>
        <div class="icon">🔬</div>
        <div class="icon">📊</div>
        <div class="icon">🎯</div>
        <div class="icon">💡</div>
      </div>

      <!-- 流星效果 -->
      <div class="meteors">
        <div class="meteor"></div>
        <div class="meteor"></div>
        <div class="meteor"></div>
      </div>

      <!-- 气泡效果 -->
      <div class="bubbles">
        <div class="bubble"></div>
        <div class="bubble"></div>
        <div class="bubble"></div>
        <div class="bubble"></div>
        <div class="bubble"></div>
        <div class="bubble"></div>
        <div class="bubble"></div>
        <div class="bubble"></div>
        <div class="bubble"></div>
        <div class="bubble"></div>
      </div>
    </div>
    <!-- 登录表单 -->
    <div class="view-account-container">
      <div class="form-header">
        <div class="login-title">注册和通账号</div>
        <p>请填写真实信息！</p>
      </div>
      <div class="view-account-form">
        <n-form
          ref="formRef"
          label-placement="left"
          size="large"
          :model="formInline"
          :rules="rules"
        >
          <n-form-item path="username">
            <n-input
              class="custom-input"
              v-model:value="formInline.username"
              placeholder="用户名"
            />
          </n-form-item>
          <n-form-item path="phone">
            <n-input class="custom-input" v-model:value="formInline.phone" placeholder="手机号码" />
          </n-form-item>
          <n-form-item path="password">
            <n-input
              class="custom-input"
              v-model:value="formInline.password"
              type="password"
              showPasswordOn="click"
              placeholder="这里设置密码"
            />
          </n-form-item>
          <n-form-item path="captcha">
            <div class="verification-code-container">
              <n-input
                class="custom-input verification-input"
                v-model:value="formInline.captcha"
                placeholder="验证码"
                @keyup.enter="handleRegister"
              />
              <n-button
                class="send-code-btn"
                type="primary"
                :disabled="!canSendCode || countdown > 0"
                :bordered="false"
                @click="openVerifyModel"
                :loading="sendingCode"
              >
                {{ countdown > 0 ? `${countdown}s` : '发送验证码' }}
              </n-button>
            </div>
          </n-form-item>
          <n-form-item style="margin-top: 20px">
            <n-button
              class="login-btn"
              type="primary"
              :bordered="false"
              :loading="loading"
              @click="handleRegister"
              block
            >
              注册
            </n-button>
          </n-form-item>
        </n-form>
        <div class="signup-link">
          已有账户？<a href="javascript:" @click="handleLogin">立即登录</a>
        </div>
      </div>
    </div>
  </div>

  <n-modal v-model:show="isShowSliderVerify" transform-origin="center">
    <slider-verify ref="sliderVerify" @success="onSuccess" @fail="onFail" />
  </n-modal>
</template>

<script lang="ts" setup>
  import { reactive, ref, computed } from 'vue';
  import { useRouter } from 'vue-router';
  import { useMessage, FormItemRule } from 'naive-ui';
  // import { getCerCode } from '@/api/login/login';
  import { register, sendCaptchaApi } from '@/api/system/user';
  import { ResultEnum } from '@/enums/httpEnum';
  import SliderVerify from '@/views/components/SliderVerify.vue';

  interface FormState {
    username: string;
    phone: string;
    password: string;
    captcha: string;
    registerSource: string;
  }

  const formRef = ref();
  const message = useMessage();
  const loading = ref(false);
  const sendingCode = ref(false);
  const countdown = ref(0);

  const formInline = reactive({
    username: '',
    phone: '',
    password: '',
    captcha: '',
  });

  const rules = {
    phone: {
      key: 'phone',
      required: true,
      trigger: 'blur',
      validator: (_rule: FormItemRule, value: string) => {
        return new Promise<void>((resolve, reject) => {
          if (!value) {
            reject(Error('请输入手机号码')); // reject with error message
          } else {
            const reg = /^1(3|4|5|6|7|8|9)\d{9}$/;
            if (reg.test(value)) {
              resolve();
            } else {
              reject(Error('请输入正确的手机号码'));
            }
          }
        });
      },
    },
    password: {
      required: true,
      trigger: 'blur',
      validator: (_rule: FormItemRule, value: string) => {
        return new Promise<void>((resolve, reject) => {
          if (!value) {
            reject(Error('请输入密码')); // reject with error message
          } else {
            const reg = /^[A-Za-z0-9]{6,20}$/;
            if (reg.test(value)) {
              resolve();
            } else {
              reject(Error('密码应为6到20位字母、数字组合'));
            }
          }
        });
      },
    },
    captcha: {
      required: true,
      message: '请输入验证码',
      trigger: 'blur',
    },
  };

  const router = useRouter();

  // 计算是否可以发送验证码
  const canSendCode = computed(() => {
    const reg = /^1(3|4|5|6|7|8|9)\d{9}$/;
    return reg.test(formInline.phone);
  });

  const isShowSliderVerify = ref(false);

  const openVerifyModel = () => {
    if (!canSendCode.value) {
      message.warning('请输入正确的手机号码');
      return;
    }
    isShowSliderVerify.value = true;
  };

  /* 滑动验证成功*/
  const onSuccess = (captcha: any) => {
    let params = {
      phone: formInline.phone,
      ...captcha,
    };
    setTimeout(() => {
      isShowSliderVerify.value = false;
      sendVerificationCode(params);
    }, 1000);
  };
  /* 滑动验证失败*/
  const onFail = (msg: any) => {
    // message.error(msg);
  };

  // 发送验证码，调验证码接口
  const sendVerificationCode = async (data: any) => {
    sendingCode.value = true;
    try {
      await sendCaptchaApi(data);

      message.success('验证码已发送');

      // 开始倒计时
      countdown.value = 60;
      const timer = setInterval(() => {
        countdown.value--;
        if (countdown.value <= 0) {
          clearInterval(timer);
        }
      }, 1000);
    } catch (error) {
      // message.error('发送验证码失败，请重试');
    } finally {
      sendingCode.value = false;
    }
  };

  const handleRegister = async () => {
    formRef.value.validate(async (errors: any) => {
      if (!errors) {
        try {
          const { username, phone, password, captcha } = formInline;
          message.loading('注册中...');
          loading.value = true;

          const params: FormState = {
            username,
            phone,
            password,
            captcha,
            registerSource: 'web',
          };
          const { code, message: msg } = await register(params);
          message.destroyAll();
          if (code == ResultEnum.SUCCESS) {
            message.success('注册成功,可返回登录页登录');
            // router.replace('/login');
          } else {
            message.info(msg || '注册失败');
          }
        } finally {
          loading.value = false;
        }
      } else {
        message.error('请填写完整信息，并且进行验证码校验');
      }
    });
  };

  const handleLogin = () => {
    router.replace('/login');
  };
</script>

<style lang="less" scoped>
  /* 基础样式 */
  .login-container {
    min-height: 100vh;
    background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    overflow: hidden;
  }

  .login-container::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: radial-gradient(circle at 20% 80%, rgba(74, 144, 226, 0.1) 0%, transparent 50%),
      radial-gradient(circle at 80% 20%, rgba(80, 200, 120, 0.1) 0%, transparent 50%),
      radial-gradient(circle at 40% 40%, rgba(255, 107, 107, 0.1) 0%, transparent 50%);
    z-index: 0;
  }

  .background-elements {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 1;
  }

  /* 几何图形样式 */
  .geometric-shapes {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
  }

  .triangle {
    position: absolute;
    top: 20%;
    left: 15%;
    width: 0;
    height: 0;
    border-left: 15px solid transparent;
    border-right: 15px solid transparent;
    border-bottom: 26px solid rgba(74, 144, 226, 0.3);
    animation: float 6s ease-in-out infinite;
  }

  .circle {
    position: absolute;
    top: 60%;
    right: 20%;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: rgba(80, 200, 120, 0.3);
    animation: float 8s ease-in-out infinite reverse;
  }

  .hexagon {
    position: absolute;
    top: 30%;
    right: 10%;
    width: 30px;
    height: 17px;
    background: rgba(255, 107, 107, 0.3);
    position: relative;
    animation: rotate 10s linear infinite;
  }

  .hexagon:before,
  .hexagon:after {
    content: '';
    position: absolute;
    width: 0;
    border-left: 15px solid transparent;
    border-right: 15px solid transparent;
  }

  .hexagon:before {
    bottom: 100%;
    border-bottom: 8.5px solid rgba(255, 107, 107, 0.3);
  }

  .hexagon:after {
    top: 100%;
    border-top: 8.5px solid rgba(255, 107, 107, 0.3);
  }

  .square {
    position: absolute;
    top: 70%;
    left: 25%;
    width: 25px;
    height: 25px;
    background: rgba(74, 144, 226, 0.3);
    animation: float 7s ease-in-out infinite;
    transform: rotate(45deg);
  }

  .diamond {
    position: absolute;
    top: 40%;
    left: 5%;
    width: 20px;
    height: 20px;
    background: rgba(255, 193, 7, 0.3);
    transform: rotate(45deg);
    animation: pulse 4s ease-in-out infinite;
  }

  /* AI相关图标样式 */
  .subject-icons {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 1;
  }

  .subject-icons .icon {
    position: absolute;
    font-size: 2rem;
    opacity: 0.6;
    animation: floatSubject 8s ease-in-out infinite;
    filter: drop-shadow(0 0 10px rgba(74, 144, 226, 0.3));
  }

  .subject-icons .icon:nth-child(1) {
    top: 15%;
    left: 10%;
    animation-delay: 0s;
  }
  .subject-icons .icon:nth-child(2) {
    top: 25%;
    right: 15%;
    animation-delay: 1s;
  }
  .subject-icons .icon:nth-child(3) {
    top: 45%;
    left: 8%;
    animation-delay: 2s;
  }
  .subject-icons .icon:nth-child(4) {
    top: 35%;
    right: 20%;
    animation-delay: 3s;
  }
  .subject-icons .icon:nth-child(5) {
    top: 65%;
    left: 12%;
    animation-delay: 4s;
  }
  .subject-icons .icon:nth-child(6) {
    top: 75%;
    right: 10%;
    animation-delay: 5s;
  }
  .subject-icons .icon:nth-child(7) {
    top: 55%;
    right: 25%;
    animation-delay: 6s;
  }
  .subject-icons .icon:nth-child(8) {
    top: 85%;
    left: 20%;
    animation-delay: 7s;
  }

  /* 流星效果 */
  .meteors {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
  }

  .meteor {
    position: absolute;
    width: 4px;
    height: 4px;
    background: linear-gradient(45deg, #fff, rgba(74, 144, 226, 0.8));
    border-radius: 50%;
    box-shadow: 0 0 10px rgba(74, 144, 226, 0.8);
    animation: meteorFall 3s linear infinite;
  }

  .meteor:nth-child(1) {
    top: 10%;
    left: 10%;
    animation-delay: 0s;
  }

  .meteor:nth-child(2) {
    top: 20%;
    left: 50%;
    animation-delay: 1s;
  }

  .meteor:nth-child(3) {
    top: 30%;
    left: 80%;
    animation-delay: 2s;
  }

  /* 增强气泡效果，增加更多气泡和更明显的视觉效果 */
  .bubbles {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 2;
    overflow: hidden;
  }

  .bubble {
    position: absolute;
    bottom: -100px;
    background: linear-gradient(
      135deg,
      rgba(74, 144, 226, 0.3),
      rgba(80, 200, 120, 0.2),
      rgba(255, 107, 107, 0.2)
    );
    border-radius: 50%;
    animation: bubbleRise 8s linear infinite;
    box-shadow: 0 0 20px rgba(74, 144, 226, 0.4), inset 0 0 20px rgba(255, 255, 255, 0.2);
    border: 1px solid rgba(255, 255, 255, 0.3);
  }

  .bubble:nth-child(1) {
    left: 5%;
    width: 30px;
    height: 30px;
    animation-delay: 0s;
    animation-duration: 10s;
  }

  .bubble:nth-child(2) {
    left: 15%;
    width: 20px;
    height: 20px;
    animation-delay: 1s;
    animation-duration: 8s;
  }

  .bubble:nth-child(3) {
    left: 25%;
    width: 35px;
    height: 35px;
    animation-delay: 2s;
    animation-duration: 12s;
  }

  .bubble:nth-child(4) {
    left: 35%;
    width: 25px;
    height: 25px;
    animation-delay: 3s;
    animation-duration: 9s;
  }

  .bubble:nth-child(5) {
    left: 45%;
    width: 28px;
    height: 28px;
    animation-delay: 4s;
    animation-duration: 11s;
  }

  .bubble:nth-child(6) {
    left: 55%;
    width: 22px;
    height: 22px;
    animation-delay: 5s;
    animation-duration: 7s;
  }

  .bubble:nth-child(7) {
    left: 65%;
    width: 32px;
    height: 32px;
    animation-delay: 6s;
    animation-duration: 13s;
  }

  .bubble:nth-child(8) {
    left: 75%;
    width: 18px;
    height: 18px;
    animation-delay: 7s;
    animation-duration: 8s;
  }

  .bubble:nth-child(9) {
    left: 85%;
    width: 26px;
    height: 26px;
    animation-delay: 8s;
    animation-duration: 10s;
  }

  .bubble:nth-child(10) {
    left: 95%;
    width: 24px;
    height: 24px;
    animation-delay: 9s;
    animation-duration: 9s;
  }
  .view-account-container {
    flex: 1;
    width: 400px;
    margin: 0 auto;
    border-radius: 10px;
    overflow: hidden;
    position: absolute;
    padding: 30px;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(255, 255, 255, 0.9);
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1), 0 0 0 1px rgba(255, 255, 255, 0.2);
  }

  .form-header {
    text-align: center;
    margin-bottom: 30px;
  }

  .form-header .login-title {
    color: #2c3e50;
    font-size: 2rem;
    margin-bottom: 8px;
    background: linear-gradient(135deg, #4a90e2, #50c878);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
  }

  .form-header p {
    color: #7f8c8d;
    font-size: 0.9rem;
  }

  .form-options {
    display: flex;
    justify-content: end;
    align-items: center;
    font-size: 0.9rem;
  }

  .forgot-password {
    color: #4a90e2;
    text-decoration: none;
    transition: color 0.3s ease;
  }

  .forgot-password:hover {
    color: #357abd;
  }

  .login-btn {
    background: linear-gradient(135deg, #4a90e2, #50c878);
    border-radius: 12px;
    font-size: 1.1rem;
    cursor: pointer;
    transition: all 0.3s ease;
    height: 50px;
  }

  .login-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 10px 25px rgba(74, 144, 226, 0.3);
  }

  .login-btn:active {
    transform: translateY(0);
  }

  .signup-link {
    text-align: center;
    color: #7f8c8d;
    font-size: 0.9rem;
  }

  .signup-link a {
    color: #4a90e2;
    text-decoration: none;
    font-weight: 600;
  }

  .signup-link a:hover {
    color: #357abd;
  }

  .verification-code-container {
    display: flex;
    gap: 10px;
    align-items: center;
  }

  .verification-input {
    flex: 1;
  }

  .send-code-btn {
    background: linear-gradient(135deg, #4a90e2, #50c878);
    border: none;
    border-radius: 8px;
    color: white;
    font-size: 0.9rem;
    height: 50px;
    min-width: 120px;
    transition: all 0.3s ease;
    white-space: nowrap;
  }

  .send-code-btn:hover:not(:disabled) {
    transform: translateY(-2px);
    box-shadow: 0 10px 25px rgba(74, 144, 226, 0.3);
  }

  .send-code-btn:disabled {
    background: #ccc;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
  }

  /deep/ .n-form-item-feedback-wrapper {
    min-height: 25px !important;
  }

  .custom-input {
    height: 50px !important;
    line-height: 50px !important;
    border-radius: 10px !important;
    overflow: hidden;
    /deep/ .n-input-wrapper {
      background-color: #e9effd !important;
    }
  }

  /deep/.n-input {
    font-size: 16px;
  }

  /* 动画定义 */
  @keyframes float {
    0%,
    100% {
      transform: translateY(0px) rotate(0deg);
    }
    50% {
      transform: translateY(-20px) rotate(180deg);
    }
  }

  @keyframes rotate {
    0% {
      transform: rotate(0deg);
    }
    100% {
      transform: rotate(360deg);
    }
  }

  @keyframes pulse {
    0%,
    100% {
      transform: scale(1) rotate(45deg);
      opacity: 0.7;
    }
    50% {
      transform: scale(1.2) rotate(45deg);
      opacity: 1;
    }
  }

  @keyframes floatSubject {
    0%,
    100% {
      transform: translateY(0px) translateX(0px);
      opacity: 0.6;
    }
    25% {
      transform: translateY(-15px) translateX(10px);
      opacity: 0.8;
    }
    50% {
      transform: translateY(-10px) translateX(-5px);
      opacity: 0.7;
    }
    75% {
      transform: translateY(-20px) translateX(15px);
      opacity: 0.9;
    }
  }

  @keyframes meteorFall {
    0% {
      transform: translateX(0) translateY(0);
      opacity: 1;
    }
    100% {
      transform: translateX(300px) translateY(300px);
      opacity: 0;
    }
  }

  @keyframes bubbleRise {
    0% {
      bottom: -100px;
      opacity: 0;
      transform: translateX(0) scale(1);
    }
    10% {
      opacity: 0.7;
    }
    50% {
      opacity: 0.9;
      transform: translateX(30px) scale(1.8);
    }
    90% {
      opacity: 0.5;
      transform: translateX(-20px) scale(2.5);
    }
    100% {
      bottom: 110vh;
      opacity: 0;
      transform: translateX(50px) scale(3);
    }
  }

  /* 响应式设计 */
  @media (max-width: 768px) {
    .login-form {
      padding: 30px 20px;
      margin: 20px;
    }

    .form-header h1 {
      font-size: 1.5rem;
    }

    .subject-icons .icon {
      font-size: 1.5rem;
    }
  }
</style>
